<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="main">
        <el-input
          v-model="queryParams.main"
          placeholder="请输入关键字查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:examProjects:add']"
        >添加项目
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="examProject">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + (queryParams.pageNo - 1) * queryParams.pageSize + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目编号" align="center" prop="code"/>
      <el-table-column label="项目名称" align="center" prop="name"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUser(scope.row)"
            v-hasPermi="['business:examProjects:userEdit']"
          >用户权限
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:examProjects:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:examProjects:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增级修改项目 -->
    <el-dialog :title="title" :visible.sync="open" width="660px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目编号" prop="code">
          <el-input v-model="form.code"/>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改用户权限 -->
    <el-dialog :title="usertitle" :visible.sync="useropen" width="660px" append-to-body>
      <el-form :model="userform" label-width="80px">
        <el-transfer
          filterable
          :filter-method="filterMethod"
          filter-placeholder="请输入用户账号"
          v-model="values"
          :titles="['未选', '已选']"
          :data="data">
        </el-transfer>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUser">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import { add, del, examProjectList, getUserList, update, updateUsers } from '@/api/business/examProject/examProject'

  export default {
  name: 'examProject',
  data() {
    return {
      data: [],
      filterMethod(query, item) {
        return item.users.indexOf(query) > -1
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支持项目表格数据
      examProject: [],
      // 弹出层标题
      title: '',
      usertitle:'',
      // 是否显示弹出层
      open: false,
      useropen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        main: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: '项目编码不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '单位不能为空', trigger: 'blur' }
        ]
      },
      userform: {},
      values: [],
      id: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      examProjectList(this.queryParams).then(response => {
        this.examProject = response.data.records
        this.total = response.data.totalCount
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    // 表单重置
    resetUser() {
      this.userform = {}
      this.resetForm('userform')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    //新增
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加项目'
      this.id = null
    },
    //修改
    handleUpdate(row) {
      this.reset()
      this.form = row
      this.id = row.id
      this.open = true
      this.title = '编辑项目'
    },
    //修改用户权限
    handleUser(row) {
      console.log(row);
      this.resetUser()
      this.userform = row
      this.id = row.id
      this.useropen = true
      this.usertitle = '编辑用户权限'
      this.getUserList(row);
    },
    //获取所有用户的信息
    getUserList(row) {
      this.data = []
      this.values = []
      if(row.users){
        this.values =row.users.split(',')
      }
      console.log(this.values);
      let query = { pageNum: 1, pageSize: 20 }
      getUserList(query).then(response => {
        console.log(response)
        response.rows.forEach((user, index) => {
          this.data.push({
            label: user.userName,
            key: user.userName,
            users: user.userName
          })
        })
        console.log(this.data);
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!this.id) {
            add(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          } else {
            update(this.form, this.id).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /**提交修改权限按钮*/
    submitUser() {
      if (this.id) {
        this.userform.users = this.values.toString()
        console.log(this.userform)
        updateUsers(this.userform, this.id).then(response => {
          this.$modal.msgSuccess('修改成功')
          this.open = false
          this.getList()
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('确定删除项目["' + row.name + '"]吗？').then(function() {
        return del(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
